const express = require('express')
const router = express.Router()
const multer = require('multer')
const path = require('path')
const {baseImageUrl} = require('../config')

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 这里是在server端要放图片的目录
        cb(null, path.join(__dirname,'../static'))
    },
    filename: function (req, file, cb) {
        // 这里是对文件重命名
        let name = path.extname(file.originalname)
        cb(null,file.fieldname+'-'+Date.now()+name)
    }
})

const upload = multer({ storage: storage})

router.post('/',upload.single('file'),(req,res)=>{
    let file = req.file
    file.url = `${baseImageUrl}/public/${file.filename}`

    res.send({
        code:200,
        message:"上传成功",
        data:file
    })
})



module.exports = router
